import type {
  ConsultOrderPreParams,
  ConsultOrderPreData,
  PartialConsult,
  ConsultOrderItem,
  ConsultOrderListParams,
  ConsultOrderPage,
} from '@/types/consult'

import type {
  KnowledgePage,
  KnowledgeParams,
  DoctorPage,
  PageParams,
  FollowType,
  TopDep,
  Image,
} from '@/types/consult'
import { request } from '@/utils/request'

/**
 * 获取知识列表
 * @param params 查询参数
 * @returns Promise
 */
export const getKnowledgePage = (params: KnowledgeParams) => {
  return request.get<KnowledgePage>('/patient/home/knowledge', { params })
}

/**
 * 获取医生分页信息
 * @param params
 * @returns Promise
 */
export const getDoctorPage = (params: PageParams) => {
  return request.get<DoctorPage>('/home/page/doc', { params })
}

/**
 * 关注医生
 * @param id 医生id
 * @param type 关注类型 默认为'doc'
 * @returns Promise
 */
export const followDoctor = (id: string, type: FollowType = 'doc') => {
  return request.post('/like', { id, type })
}

/**
 * 获取所有科室
 * @returns Promise
 */
export const getAllDep = () => {
  return request.get<TopDep[]>('/dep/all')
}

/**
 * 上传图片
 * @param file 图片文件
 * @returns Promise
 */
export const uploadImage = (file: File) => {
  const fd = new FormData()
  fd.append('file', file)
  return request.post<Image>('/upload', fd)
}

/**
 * 拉取预支付订单信息
 * @param params 预支付订单参数
 * @returns
 */
export const getConsultOrderPre = (params: ConsultOrderPreParams) => {
  return request.get<ConsultOrderPreData>('/patient/consult/order/pre', {
    params,
  })
}

/**
 * 生成订单
 * @param data 订单信息
 * @returns Promise
 */
export const createConsultOrder = (data: PartialConsult) => {
  return request.post<{ id: string }>('/patient/consult/order', data)
}

/**
 * 获取支付地址
 * @param data 支付方式订单号和回调地址
 * @returns Promise
 */
export const getConsultOrderPayUrl = (data: {
  paymentMethod: 0 | 1
  orderId: string
  payCallback: string
}) => {
  return request.post<{ payUrl: string }>('/patient/consult/pay', data)
}

/**
 * 获取订单详情
 * @param orderId 订单id
 * @returns Promise
 */
export const getConsultOrderDetail = (orderId: string) => {
  return request.get<ConsultOrderItem>('/patient/consult/order/detail', {
    params: { orderId },
  })
}

/**
 * 查看处方
 * @param id 处方id
 * @returns Promise
 */
export const getPrescriptionPic = (id: string) => {
  return request.get<{ url: string }>(`/patient/consult/prescription/${id}`)
}

/**
 * 评价问诊
 * @param data
 * @returns Promise
 */
export const evaluateConsultOrder = (data: {
  docId: string // 医生ID
  orderId: string // 订单ID
  score: number
  content: string
  anonymousFlag: 0 | 1
}) => {
  return request.post<{ id: string }>('/patient/order/evaluate', data)
}

/**
 *  获取问诊订单记录列表
 * @param params 查询参数
 * @returns Promise
 */
export const getConsultOrderList = (params: ConsultOrderListParams) => {
  return request.get<ConsultOrderPage>('/patient/consult/order/list', {
    params,
  })
}

/**
 * 取消订单
 * @param id 订单id
 * @returns Promise
 */
export const cancelOrder = (id: string) => {
  return request.put(`/patient/order/cancel/${id}`)
}

/**
 * 删除订单
 * @param id 订单id
 * @returns Promise
 */
export const deleteOrder = (id: string) => {
  return request.delete(`/patient/order/${id}`)
}
